#include <stdio.h>

typedef unsigned char N1;
typedef unsigned long int N4;
typedef   signed long int I4;

#define MEMLEN 4096ul
#define VARLEN MEMLEN

#define BEG             do {
#define END             } while(0)
#define ERR1(s,n1)      BEG fprintf(stderr,s,n1); \
                        fflush(stdout); fflush(stderr); \
                        return (-1); END

static I4 mem[MEMLEN]={};
static I4 var[VARLEN]={};

typedef struct _tDig
{ N4 *m,*pos,*num;
  N1 *dig,*base;
} tDig;

void print(I4 num)
{ printf("%li  %lu  0x%08lX\n",
    num,num,num);
}
I4 digit(tDig *dg)
{ if (!*dg->pos)
    ERR1("Error: Memory: %lu\n",*dg->m);
  mem[--*dg->pos]=*dg->num;
  *dg->num=0; *dg->dig=0; *dg->base=0;
  return (0);
}

int main(int argc,char **argv)
{ N1 cc; int ii; I4 n1,n2;
  N1 c1a,c1b,c2a,c2b;
  N4 pos=MEMLEN,m=1,num=0;
  N1 dig=0,base=0; tDig dg;

  c1a=(N1)'0'; c2a=(N1)'A';
  c1b=c1a+9; c2b=c2a+5;
  dg.m=&m; dg.pos=&pos; dg.num=&num;
  dg.dig=&dig; dg.base=&base;

  while (1)
  { ii=fgetc(stdin);
    if (ii==-1) break;
    else cc=(N1)ii;
    if (cc=='\n') ++m;
    switch (cc)
    { case '+':
        if (dig && digit(&dg)) return (-1);
        if (pos+2>MEMLEN)
          ERR1("Error: Operand: %lu\n",m);
        n1=mem[pos++];
        mem[pos]+=n1;
        break;
      case '-':
        if (dig && digit(&dg)) return (-1);
        if (pos+2>MEMLEN)
          ERR1("Error: Operand: %lu\n",m);
        n1=mem[pos++];
        mem[pos]-=n1;
        break;
      case '*':
        if (dig && digit(&dg)) return (-1);
        if (pos+2>MEMLEN)
          ERR1("Error: Operand: %lu\n",m);
        n1=mem[pos++];
        mem[pos]*=n1;
        break;
      case '/':
        if (dig && digit(&dg)) return (-1);
        if (pos+2>MEMLEN)
          ERR1("Error: Operand: %lu\n",m);
        if (!(n1=mem[pos++]))
          ERR1("Error : Div 0: %lu\n",m);
        mem[pos]/=n1;
        break;
      case '%':
        if (dig && digit(&dg)) return (-1);
        if (pos+2>MEMLEN)
          ERR1("Error: Operand: %lu\n",m);
        if (!(n1=mem[pos++]))
          ERR1("Error : Div 0: %lu\n",m);
        mem[pos]%=n1;
        break;
      case '~':
        if (dig && digit(&dg)) return (-1);
        if (pos+1>MEMLEN)
          ERR1("Error: Operand: %lu\n",m);
        mem[pos]=-mem[pos];
        break;
      case '!':
        if (dig && digit(&dg)) return (-1);
        if (pos+1>MEMLEN)
          ERR1("Error: Operand: %lu\n",m);
        mem[pos]=~mem[pos];
        break;
      case '&':
        if (dig && digit(&dg)) return (-1);
        if (pos+2>MEMLEN)
          ERR1("Error: Operand: %lu\n",m);
        n1=mem[pos++];
        mem[pos]&=n1;
        break;
      case '|':
        if (dig && digit(&dg)) return (-1);
        if (pos+2>MEMLEN)
          ERR1("Error: Operand: %lu\n",m);
        n1=mem[pos++];
        mem[pos]|=n1;
        break;
      case '^':
        if (dig && digit(&dg)) return (-1);
        if (pos+2>MEMLEN)
          ERR1("Error: Operand: %lu\n",m);
        n1=mem[pos++];
        mem[pos]^=n1;
        break;
      case '<':
        if (dig && digit(&dg)) return (-1);
        if (pos+2>MEMLEN)
          ERR1("Error: Operand: %lu\n",m);
        n1=mem[pos++];
        mem[pos]<<=n1;
        break;
      case '>':
        if (dig && digit(&dg)) return (-1);
        if (pos+2>MEMLEN)
          ERR1("Error: Operand: %lu\n",m);
        n1=mem[pos++];
        mem[pos]>>=n1;
        break;
      case '?':
        if (dig && digit(&dg)) return (-1);
        if (pos+1>MEMLEN)
          ERR1("Error: Operand: %lu\n",m);
        n1=mem[pos];
        if (n1<0 || n1>=VARLEN)
          ERR1("Error: Variable: %lu\n",m);
        mem[pos]=var[n1];
        break;
      case '=':
        if (dig && digit(&dg)) return (-1);
        if (pos+2>MEMLEN)
          ERR1("Error: Operand: %lu\n",m);
        n2=mem[pos++]; n1=mem[pos++];
        if (n2<0 || n2>=VARLEN)
          ERR1("Error: Variable: %lu\n",m);
        var[n2]=n1;
        break;
      case 'h':
        if (dig!=1 || num)
          ERR1("Error: Number: %lu\n",m);
        base=16; break;
      default:
        switch (base)
        { case 16:
            if (c1a<=cc && cc<=c1b) cc-=c1a; else
            if (c2a<=cc && cc<=c2b) cc-=c2a-10;
            else ii=-1; if (ii!=-1)
            { num=(num<<4)|cc;
              ++dig; continue;
            } break;
          case 10:
          default:
            if (c1a<=cc && cc<=c1b)
            { num=(num*10)+(cc-c1a);
              ++dig; continue;
            } break;
        }
        if (dig)
        { if (!pos)
            ERR1("Error: Memory: %lu\n",m);
          mem[--pos]=num;
          num=0; dig=0; base=0;
        }
        if (cc=='\n' && pos<MEMLEN)
        { print(mem[pos++]);
          continue;
        }
    }
  }
  if (dig)
  { if (!pos)
      ERR1("Error: Memory: %lu\n",m);
    mem[--pos]=num;
  }
  if (pos<MEMLEN)
    print(mem[pos++]);
  fflush(stdout);

  return (0);
}
